module Main where
-- composition of functional 
import Control.Monad.ST
import Data.STRef
import Data.IORef
import qualified Data.Map.Lazy as Map

import qualified FLC 

import Control.Parallel.Strategies
import Data.Time.Clock 



counterIO :: IO (IO Int)
counterIO = do 
  c<-newIORef 0
  return $ do
        modifyIORef c (+1)
        readIORef c


-- memoize是得函数f具有记忆功能，加快f的速度
-- 也就是记住f的映射值，从而加快速度
-- 
-- 
--
memoize :: Ord k => (k -> ST s a) -> ST s (k -> ST s a)
memoize f = do
    mc <- newSTRef Map.empty
    return $ \k -> do
        c <- readSTRef mc
        case Map.lookup k c of
            Just a -> return a
            Nothing -> do a <- f k
                          writeSTRef mc (Map.insert k a c) >> return a


tent_map_list :: Int ->Double->[Double]
tent_map_list n y= my_fun (n-1) [FLC.tent_map y]
  where 
    my_fun:: Int->[Double]->[Double]
    my_fun 0 x= x
    my_fun nn x= my_fun (nn-1) ( (FLC.tent_map (head x)  ):x )

logistic_tent_map_list :: Int ->Double->[Double]
logistic_tent_map_list n y= my_fun (n-1) [FLC.logistic_tent_map y]
  where 
    my_fun:: Int->[Double]->[Double]
    my_fun 0 x= x
    my_fun nn x= my_fun (nn-1) ( (FLC.logistic_tent_map (head x)  ):x )


difftimetofloat:: DiffTime->Float
difftimetofloat a= realToFrac a 




main :: IO ()
main = do 
  f<-counterIO 
  print=<< f
  print =<< f 
  putStrLn "Hello, Haskell!"

  putStrLn $ show $ FLC.logistic_map_list 100 0.4931

  time1<-  return.difftimetofloat.  utctDayTime. addUTCTime (60*60*8) =<< getCurrentTime
  putStrLn $ show  time1 

  
  FLC.someFunc
  time2<-  return.difftimetofloat.  utctDayTime. addUTCTime (60*60*8) =<< getCurrentTime

  putStrLn $ show (time2-time1)
